package com.lm56.web;

import com.lm56.common.MyTextValuePair;
import com.lm56.web.network.domain.CompanyDTO;
import com.lm56.web.network.domain.Location;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kelvin Yeung on 2016/10/26.
 */
@Controller
@RequestMapping("/common")
public class CommonCotroller {

    Logger logger = LoggerFactory.getLogger(CommonCotroller.class);


    @Autowired
    CommonService commonService;

    @RequestMapping("location/textvaluepair")
    @ResponseBody
    public List<MyTextValuePair<Integer>> getLocationJsonForTagsInput(@ModelAttribute("ids") String ids, HttpServletRequest request,HttpServletResponse response){
       if(StringUtils.isNotBlank(ids)){
           String[] idArray = ids.split(",");
           List<Long> idsList = new ArrayList<Long>();
           for(String id:idArray){
               try {

                 Long  tmpId = Long.parseLong(id);
                 idsList.add(tmpId);
               }catch (NumberFormatException e){
                return  new ArrayList<MyTextValuePair<Integer>>();
               }

           }
           return  commonService.getLocationsKeyValueListByIds(idsList);
       }

       //response.setHeader("","");
        response.setHeader("X-Frame-Options", "SAMEORIGIN");
        response.setHeader("Access-Control-Allow-Origin", "*");
        return commonService.getLocationsKeyValueList();
    }



    @RequestMapping("goods/qty/unit")
    @ResponseBody
    public List<MyTextValuePair<String>> getGoodsQtyUnit(HttpServletRequest request,HttpServletResponse response){
        //response.setHeader("","");
        response.setHeader("X-Frame-Options", "SAMEORIGIN");
        response.setHeader("Access-Control-Allow-Origin", "*");
        return commonService.getGoodsQtyUnit();
    }


    @RequestMapping("company/list")
    @ResponseBody
    public List<CompanyDTO> getCompanyList(){
        return  commonService.getCompanyList();
    }


}
